package com.elec.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.elec.domain.R;
import com.elec.mapper.AlarmRecordMapper;
import com.elec.domain.entity.AlarmRecord;
import com.elec.service.AlarmRecordService;
import org.springframework.stereotype.Service;

/**
 * 记录所有用电异常报警(AlarmRecord)表服务实现类
 *
 * @author yu
 * @since 2024-07-05 13:42:59
 */
@Service("alarmRecordService")
public class AlarmRecordServiceImpl extends ServiceImpl<AlarmRecordMapper, AlarmRecord> implements AlarmRecordService {
    public void test(){

    }

    @Override
    public R add(AlarmRecord alarmRecord) {
        if (save(alarmRecord)) {
            return R.success("添加成功");
        }
        return R.error("添加失败");
    }

    @Override
    public R delete(Long id) {
        if (removeById(id)) {
            return R.success("删除成功");
        }
        return R.error("删除失败");
    }

    @Override
    public R get(Long id) {
        if (getById(id) != null) {
            return R.success(getById(id));
        }
        return R.error("查询失败");
    }

    @Override
    public R getList(Integer pageNum, Integer pageSize, AlarmRecord alarmRecord) {
        LambdaQueryWrapper<AlarmRecord> queryWrapper = new LambdaQueryWrapper<>();
        if (alarmRecord != null) {
            if (alarmRecord.getAlarmType() != null && !alarmRecord.getAlarmType().equals("")) {
                queryWrapper.eq(AlarmRecord::getAlarmType, alarmRecord.getAlarmType());
            }
            if (alarmRecord.getDataId() != null) {
                queryWrapper.eq(AlarmRecord::getDataId, alarmRecord.getDataId());
            }
            if (alarmRecord.getDescription() != null) {
                queryWrapper.eq(AlarmRecord::getDescription, alarmRecord.getDescription());
            }
        }
        queryWrapper.orderByDesc(AlarmRecord::getTimestamp);
        Page<AlarmRecord> page = new Page<>(pageNum, pageSize);
        page(page, queryWrapper);
        return R.success(page.getRecords(), page.getTotal());
    }

    @Override
    public R update(AlarmRecord alarmRecord) {
        if (updateById(alarmRecord)) {
            return R.success("修改成功");
        }
        return R.error("修改失败");
    }
}


